/*******************************Patenting Intensity********************************
Main purpose: compare patenting activities between the USPTO and EPO
datasets:
tls201_ep: all patents filed at the EPO, obtained from PATSTAT 2017 (table 201)
tls209ipcmain: primary IPC subclass (4-digit code) for all patent applications, obtained from PATSTAT 2017 (table 209)
us9803: patent applications (granted or ungranted) filed at the USPTO, obtained from the USPTO
*******************************Patenting Intensity********************************/




********************patenting intensity at EPO by technology classes********************
//count applications (granted or ungranted) filed at the EPO (includes abandoned/pending applications)
use "$DATA\tls201_ep",clear 
keep if appln_kind=="A"
keep if appln_filing_date>=td(1jan1998) & appln_filing_date<=td(31dec2003)
gen month=mofd(appln_filing_date)
format month %tm

bys month: gen numapp=_N
bys month: egen numgrant=count(appln_filing_date) if grant==1 //granted patents
sort month numgrant
by month: replace numgrant=numgrant[_n-1] if numgrant==.
bys month: keep if _n==1
keep month num*
saveold "$DATA\numapp_epo", replace


********************patenting intensity at EPO by technology classes********************
//count applications (granted or ungranted) filed at the EPO (includes abandoned/pending applications) by IPC4
use "$DATA\tls201_ep",clear 
keep if appln_kind=="A"
keep if appln_filing_date>=td(1jan1998) & appln_filing_date<=td(31dec2003)
gen month=mofd(appln_filing_date)
format month %tm
count
joinby appln_id using "$DATA\tls209ipcmain", unmatched(master)
tab _merge //only 149 unmatched
keep if _merge==3
drop _merge
rename ipcmain ipc4
gen abn0=1-grant
bys month ipc4: egen abn=mean(abn0)
bys month ipc4: gen numapp=_N
bys month ipc4: egen numgrant=count(appln_filing_date) if grant==1 //granted patents
sort month ipc4 numgrant
by month ipc4: replace numgrant=numgrant[_n-1] if numgrant==.
bys month ipc4: keep if _n==1
keep month num* abn ipc4
replace numgrant=0 if numgrant==.
gen pat_office="EP"
saveold "$DATA\numapp_epo_ipc4", replace





**********************************Figure 11: Patenting Intensity******************************
//count applications (granted or ungranted) filed at the USPTO (includes abandoned/pending applications)
//cannot use PATSTAT's us applications bcs it does not include abandoned/pending applications filed before AIPA or filed after AIPA but opt out of the pre-grant publication requirements
use "$DATA\us9803",clear
bys month: gen numapp=_N
bys month: egen numgrant=count(appl_dt) if dpatent==1 //granted patents
sort month numgrant
by month: replace numgrant=numgrant[_n-1] if numgrant==.
bys month: keep if _n==1
keep month num*
gen us=1
append using "$DATA\numapp_epo"



global chg=tm(2000m12) //add vertical line 491:=tm(2000m12)
preserve
graph twoway (scatter numapp month if us==1) ///
(scatter numapp month if us!=1, msymbol(X)) ///
, xline(${chg}, lcolor(gray) lpattern("shortdash") lwidth(thick) ) /// 
title("US v. EP Patenting Intensity", siz(med)) ///
legend( region(color(white)) c(2) order(1 "# US applications" 2 "# EP applications") size(medsmall)) ///
yla( , nogrid) xsize(18) ysize(15) xtitle("") ytitle("")  ///
plotregion(fcolor(white)) graphregion(color(white)) bgcolor(white) 
graph export "$DATA\ep_us_app.eps", replace
restore




preserve
graph twoway (scatter numgrant month if us==1) ///
(scatter numgrant month if us!=1, msymbol(X)) ///
, xline(${chg}, lcolor(gray) lpattern("shortdash") lwidth(thick) ) /// 
title("US v. EP Patenting Intensity", siz(med)) ///
legend( region(color(white)) c(2) order(1 "# US Grants" 2 "# EP Grants") size(medsmall)) ///
yla( , nogrid) xsize(18) ysize(15) xtitle("") ytitle("")  ///
plotregion(fcolor(white)) graphregion(color(white)) bgcolor(white) 
graph export "$DATA\ep_us_pat.eps", replace
restore





**********************************Table 11: Patenting Intensity******************************

use "$DATA\us9803",clear
capture drop month
gen month=mofd(appl_dt)
format month %tm
*get IPC4 for US patents
joinby appln_id using "$DATA\tls209ipcmain", unmatched(master) //unmatched mostly due to abandoned/pending applications filed before AIPA
tab _merge 
drop _merge
*fill missing ipc4 based on the unmissing matches
gen artsub2=substr(artsub,1,3)
gsort artcls artsub2 -ipcmain
by artcls artsub2: replace ipcmain=ipcmain[_n-1] if ipcmain=="" 
gsort artcls -ipcmain
by artcls: replace ipcmain=ipcmain[_n-1] if ipcmain=="" 
drop artsub2
rename ipcmain ipc4

bys month ipc4: egen abn=mean(dabandon)
bys month ipc4: gen numapp=_N
bys month ipc4: egen numgrant=count(appl_dt) if dpatent==1 //granted patents
sort month ipc4 numgrant
by month ipc4: replace numgrant=numgrant[_n-1] if numgrant==.
bys month ipc4: keep if _n==1
keep month num* abn ipc4
replace numgrant=0 if numgrant==.
drop if missing(ipc4) //74 obs
gen pat_office="US"
append using "$DATA\numapp_epo_ipc4"
egen id=group(pat_office ipc4)
tsset id month
tsfill //add 7055 obs
replace numgrant=0 if numgrant==.
replace numapp=0 if numapp==.
gen abn0=abn
replace abn0=0 if abn0==. 
label var abn "average abn rate"
label var abn0 "average abn rate, 0 if no patents filed"
gsort id -pat_office
bys id: replace pat_office=pat_office[_n-1] if pat_office==""
gsort id -ipc4
bys id: replace ipc4=ipc4[_n-1] if ipc4==""
gen post=(month>=mofd(td(1dec2000)))
bys ipc4: egen both=nvals(post)
keep if both==2 
drop both
gen us=pat_office=="US"

preserve
bys us month: egen numapp_tot=total(numapp)
bys  us month: egen numgrant_tot=total(numgrant)
keep us month post num*tot
duplicates drop
reg numapp_tot i.us##i.post, vce(robust)
outreg2 using "$DATA\table11.xls", replace bdec(3) ctitle(App)
reg numgrant_tot i.us##i.post, vce(robust)
outreg2 using "$DATA\table11.xls", bdec(3) ctitle(Grant)
restore
reghdfe numapp i.us##i.post, absorb(ipc4) vce(robust)
outreg2 using "$DATA\table11.xls", bdec(3) ctitle(APP)
reghdfe numgrant i.us##i.post, absorb(ipc4) vce(robust)
outreg2 using "$DATA\table11.xls", bdec(3) ctitle(Grant)

reghdfe numapp i.us##i.post, absorb(ipc4 month) vce(robust)
outreg2 using "$DATA\table11.xls", bdec(3) ctitle(APP)
reghdfe numgrant i.us##i.post, absorb(ipc4 month) vce(robust)
outreg2 using "$DATA\table11.xls", bdec(3) ctitle(Grant)
